﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Gift.DAL.crm;
using Gift.Model.crm;
using Gift.Model.crm.eNum;

namespace WebSite.crm.goods
{
    public partial class brandAdd : BaseCrm
    {
        readonly t_DALBrand theBrand = new t_DALBrand();
        t_Brand oBrand;

        //bool UploadPhoto()
        //{
            //if (this.fileBrandPhoto.FileName.Contains("\\") || this.fileBrandPhoto.FileName.Contains("/"))
            //{
            //    string fileName = this.fileBrandPhoto.ShortFileName;
            //    fileName = fileName.Replace(":", "_").Replace(" ", "_").Replace("\\", "_").Replace("/", "_");
            //    fileName = DateTime.Now.Ticks.ToString() + "_" + fileName;
            //    if (this.fileBrandPhoto.HasFile)
            //    {
            //        this.fileBrandPhoto.SaveAs(Server.MapPath("~/uploadfile/product/" + fileName));
            //    }
            //    this.lblPhotoUrl.Text = fileName;
            //    return true;
            //}
            //else
            //{
            //    ExtAspNet.Alert.Show("请选择上传的图片！");
            //}
            //return false;
      //  }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.ValidateLoginRule(eRule.BrandAdd);
                if (base.Request.QueryString["bid"] != null)
                {
                    this.upbind();
                }
            }
        }

        private void upbind()
        {
            oBrand = theBrand.GetModel(int.Parse(Request.QueryString["bid"]));
            if (oBrand != null)
            {

                this.txtBrandName.Text = oBrand.brandName;
                this.txtOrder.Text = oBrand.brandOrder.ToString();
                this.isVisible.Checked = oBrand.visible;
                //if (oBrand.brandLogo != null)
                //{
                //    this.lblPhotoUrl.Text = oBrand.brandLogo.ToString();
                //    this.fileBrandPhoto.Required = false;
                //    this.fileBrandPhoto.ShowRedStar = false;
                //}
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (base.Request.QueryString["bid"] != null)
            {
                //if (this.fileBrandPhoto.FileName.Trim().Length > 0)
                //{
                //    this.UploadPhoto();
                //}
                oBrand = theBrand.GetModel(int.Parse(Request.QueryString["bid"]));
                oBrand.brandName = this.txtBrandName.Text.Trim();
                oBrand.brandOrder = int.Parse(this.txtOrder.Text);
              //  oBrand.brandLogo = this.lblPhotoUrl.Text;
                oBrand.visible = this.isVisible.Checked;
                theBrand.Update(oBrand);
                ExtAspNet.Alert.Show("商品品牌修改成功！", "", "openNewTab('BrandManager','/crm/goods/brandManager.aspx','品牌列表','BrandAdd')");
            }
            else
            {
                //this.UploadPhoto();
                oBrand = new t_Brand();
                oBrand.brandName = this.txtBrandName.Text.Trim();
                oBrand.brandOrder = int.Parse(this.txtOrder.Text);
               // oBrand.brandLogo = this.lblPhotoUrl.Text;
                oBrand.visible = this.isVisible.Checked;
                oBrand.addTime = DateTime.Now;
                theBrand.Add(oBrand);

                ExtAspNet.Alert.Show("商品品牌添加成功！", "", "openNewTab('BrandManager','/crm/goods/brandManager.aspx','品牌列表','BrandAdd')");
            }
        }

    }
}